package mytaxis.move.app;

import mytaxis.move.R;
import mytaxis.move.calendar.CalendarActivity;
import mytaxis.move.calendar.PendingRequestsActivity;
import mytaxis.move.map.DriverMap;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MenuActivity extends Activity implements OnItemClickListener {

	private String[] list;	
	private MyTaxisService mBoundService;
	private MyTaxisApplication app;
	private Drawable green; 
	private Drawable orange; 
	private Drawable red; 
	private ImageView img;

	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			mBoundService = ((MyTaxisService.LocalBinder)service).getService();
		}

		public void onServiceDisconnected(ComponentName className) {
			mBoundService = null;
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.menu);

		ListView lv = (ListView) findViewById(R.id.menulist);
		list = getResources().getStringArray(R.array.MainMenu);
		lv.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, list));
		lv.setOnItemClickListener(this);

		bindService(new Intent(this, MyTaxisService.class), mConnection, Context.BIND_AUTO_CREATE);	

		green = getResources().getDrawable(R.drawable.green_dot);
		red = getResources().getDrawable(R.drawable.red_dot);
		orange = getResources().getDrawable(R.drawable.orange_dot);
		img = (ImageView) findViewById(R.id.availableImg);
		app = (MyTaxisApplication) getApplication();
	}

	@Override
	protected void onResume() {
		super.onResume();
		doRegister();
		if(app.hasRequest()) img.setImageDrawable(orange);
		else if(app.isAvailable()) img.setImageDrawable(green);
		else img.setImageDrawable(red);
	}

	@Override
	protected void onPause() {
		super.onPause();
		doUnRegister();
	}

	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		String option = ((TextView) view).getText().toString();
		if (option.equals(list[0])) {
			Intent i = new Intent(this, DriverMap.class);
			if(mBoundService.hasActiveRequest()){
				Bundle b = mBoundService.getRequestData();
				if(b!=null) i.putExtras(b);
			}
			startActivity(i);
		} else if (option.equals(list[1])) {
			Intent i = new Intent(this, CalendarActivity.class);
			startActivity(i);
		} else if (option.equals(list[2])) {
			if(app.hasRequest()){
				Toast.makeText(this,R.string.switch_with_task,0).show();
			}else{
				app.setAvailable(!app.isAvailable());
				mBoundService.setAvailable(app.isAvailable());
				if(app.isAvailable()) img.setImageDrawable(green);
				else img.setImageDrawable(red);
			}
		}else if(option.equals(list[3])){
			Intent i = new Intent(this, PendingRequestsActivity.class);
			startActivity(i);
		} else if (option.equals(list[4])) {
			if(mBoundService.logout()){
				Toast.makeText(getApplicationContext(),R.string.logout_ok,0).show();
				Intent i = new Intent(this, Login.class);
				startActivity(i);
				finish();
			}else{
				Toast.makeText(getApplicationContext(),R.string.logout_ko,0).show();
			}
		}
	}

	private BroadcastReceiver myReceiverSol = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle extras = intent.getExtras();				
			String data = extras.getString(MyTaxisService.IMM_REQ_DATA);
			if(data.equals("1")){
				Intent i = new Intent(MenuActivity.this, RequestReceivedActivity.class);
				startActivity(i);
			}
		}
	};

	private void doRegister(){
		IntentFilter filter = new IntentFilter(MyTaxisService.IMM_REQ_FILTER);
		registerReceiver(myReceiverSol, filter);
	}

	private void doUnRegister(){
		unregisterReceiver(myReceiverSol);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unbindService(mConnection);
	}

}
